Page({
  data: {
    passport: {},
  },
  onLoad({ id }) {
    // 获取访客详情
    this.getPassport(id)
  },
  // 获取访客详情（通行证）
  async getPassport(id) {
    // 调用接口
    const  passport  = await wx.http.get('/visitor/' + id)
    // 渲染数据
    this.setData({ passport })
  },
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index',
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
   // 保存二维码
   async saveQRCode() {
    this.authSetting()
    try {
      // 读取图片信息
      const { path } = await wx.getImageInfo({
        // 二维码的图片路径
        src: this.data.passport.url,
      })
      // 保存图片到相册
      wx.saveImageToPhotosAlbum({ filePath: path })
    } catch (err) {
      wx.utils.toast('保存图片失败，稍后重试!')
    }
  },
  //授权检测,官网声明支持 Promise 风格，实际不支持
  authSetting(){

    wx.getSetting({
      success: ({authSetting})=>{
        if(authSetting['scope.writePhotosAlbum']===false){
          wx.showModal({
            title: '没有权限',
            content: '请允许添加到相册',
            success:({confirm})=>{
              if(confirm){
                wx.openSetting({
                  success:({authSetting})=>{
                    // console.log(authSetting)
                    if(authSetting['scope.writePhotosAlbum']){
                      this.saveQRCode()
                    }
                  }
                })
              }
              
            }
          })
        }
      }
    })
  }
})
